package com.heimali.happyreader.wraper;

import java.net.URI;

import com.heimali.happyreader.Statics;
import com.heimali.happyreader.config.Constants;
import com.heimali.happyreader.http.JsonParser;
import com.heimali.happyreader.model.Article;
import com.heimali.happyreader.utils.HttpUtils;

public class ArticleWraper extends BaseWraper {

	public ArticleWraper(URI uri, Callback callback) {
		super(uri, callback);
	}

	public ArticleWraper(int articleID, Callback callback) {
		super();
		this.uri = HttpUtils.buildURI(
				Constants.articleIntfPath,
				HttpUtils.buildQuery(new String[] { "aid", String.valueOf(articleID) },
						new String[] { "id", Statics.getUid() }, new String[] { "token", Statics.getToken() }));
		this.callback = callback;
	}

	@Override
	protected void onExecute(String result) {
		try {
			Article article = JsonParser.articleParser(result);
			if (article == null) {
				callback.onFail(Constants.E_SERVER_BUSY, null);
				return;
			}

			callback.onSuccess(article);

		} catch (Exception e) {
			callback.onFail(Constants.E_UNKNOWN_ERROR, e.getMessage());
		}
	}

}
